﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Input;

namespace Leuphana.GuidedTour.App.ViewModels
{
    public class TourViewModel : ViewModelBase
    {
        // Constructor
        public TourViewModel()
        {
            HotSpots = new ObservableCollection<HotSpotViewModel>();
            Highlights = new ObservableCollection<HotSpotViewModel>();

            NavigateToCommand = new NavigationCommand("tour", "type");
        }

        public ObservableCollection<HotSpotViewModel> HotSpots { get; private set; }
        public ObservableCollection<HotSpotViewModel> Highlights { get; private set; }

        public ICommand NavigateToCommand { get; private set; }

        public void LoadData()
        {
            var worker = new BackgroundWorker();
            worker.DoWork += (sender, e) =>
            {
                var formatProvider = new CultureInfo("en-US");

                // Reset collection before refilling
                var newHotSpots = new ObservableCollection<HotSpotViewModel>();
                var newHighlights = new ObservableCollection<HotSpotViewModel>();

                HotSpotViewModel prev = null;

                // Get HotSpot data
                foreach (var item in TourDataLoader.Current.LoadHotSpots())
                {
                    // Create HotSpot
                    var viewModel = new HotSpotViewModel()
                    {
                        ID = Int32.Parse(item.Attribute("ID").Value),
                        Title = item.Element("Title").Value,
                        Description = item.Element("Description").Value,
                        ImageSource = item.Element("ImageSource").Value.StartsWith("http://") ? item.Element("ImageSource").Value : "/Images/" + item.Element("ImageSource").Value,
                        Lat = Double.Parse(item.Element("Lat").Value, formatProvider),
                        Lng = Double.Parse(item.Element("Lng").Value, formatProvider)
                    };

                    // Set next item
                    if (prev != null)
                        prev.NextItem = viewModel;

                    // Set prev item
                    viewModel.PrevItem = prev;
                    prev = viewModel;

                    // AddToListing
                    newHotSpots.Add(viewModel);

                    // Add to hightlights
                    if (item.Attribute("Highlight") != null)
                    {
                        newHighlights.Add(viewModel);
                    }
                }

                // Update properties
                Deployment.Current.Dispatcher.BeginInvoke(() =>
                {
                    HotSpots = newHotSpots;
                    Highlights = newHighlights;
                    OnPropertyChanged("HotSpots");
                    OnPropertyChanged("Highlights");
                });
            };

            worker.RunWorkerAsync();
        }
    }
}
